package cn.lzj.controller.base;

import cn.lzj.pojo.*;
import cn.lzj.service.User.ProductService;
import cn.lzj.service.User.ProductTypeService;
import cn.lzj.service.User.UserService;
import cn.lzj.service.base.IndexService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;




@Controller
public class indexController {

    @Resource
    private IndexService indexService;

    @Resource
    private UserService userService;

    @Resource
    private ProductService productService;

    @Resource
    private ProductTypeService productTypeService;

    @GetMapping("/returnIndex")
    public String returnIndex(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if(user == null){
            user = new User(-1);
        }
        return getProductByPage(1, user.getUserId(), "","","","",model,request);
    }

    //未登录的网址
    @GetMapping("/index")
    public String index(Model model,HttpServletRequest request){
        //首页的信息获取---包括：商品基本信息，商品类型，用户类型的登录和注册的布局---可以做一个监听器
        User user = new User(-1,"null","null","null",
                "null","null","null","null");
        return getProductByPage(1,user.getUserId(), "","","","", model,request);
    }

    //点击换页按钮
    @GetMapping("/page/{pageNum}/{userid}")
    public String getProductByPage(@PathVariable("pageNum") Integer pageNum, @PathVariable("userid") Integer userId,
                                   String productName,
                                   String collectSearch,
                                   String orderSearch,
                                   String history,
                                   Model model,
                                   HttpServletRequest request
    ){
        HttpSession session = request.getSession();
        if(productName == null){
            productName = "";
        }
        if(collectSearch == null){
            collectSearch = "";
        }
        if(orderSearch == null){
            orderSearch = "";
        }
        if(history == null){
            history = "";
        }

        //获取类型
        List<ProductType> productTypeList = indexService.selectProductType();

        Integer pageSize = 6;
        Integer startIndex = (pageNum-1) * 6;
        Map<Integer,List<ProductBase>> map = indexService.indexPage(startIndex,pageSize,productName);
        Integer allPage = 0;
        List<ProductBase> productBaseList = null;
        for (Map.Entry<Integer, List<ProductBase>> integerListEntry : map.entrySet()) {
            allPage = integerListEntry.getKey();
            productBaseList = integerListEntry.getValue();
        }
        List<Integer> temp = new ArrayList<>();
        for(int i = 0;i<allPage;i++){
            temp.add(i+1);
        }

        model.addAttribute("allPage",allPage);
//        model.addAttribute("temps",temps);
        model.addAttribute("ProductBases",productBaseList);
        model.addAttribute("productTypeList",productTypeList);
        model.addAttribute("temp",temp);
        model.addAttribute("curPage",pageNum);

        if(userId == -1){
            session.setAttribute("user",new User(-1,"null","null","null",
                    "null","null","null","null"));


//            model.addAttribute("product",new ProductBase());
        }else {
            User user = userService.getUserById(userId);
            List<PayTable> payTableList = userService.selectOrder(user.getUserId(), orderSearch);
            List<Collect> collectList = userService.selectCollect(user, collectSearch);
            List<HistoryRecord> recordList = userService.selectHistory(user.getUserId(), history);
            model.addAttribute("recordList",recordList);
            model.addAttribute("payTableList",payTableList);
            model.addAttribute("collectList",collectList);

            session.setAttribute("user",user);
        }
        return "index";
    }


    //与分页查询关联，每次只查出6个商品
    @GetMapping("/indexSearch")
    public String indexSearch(@RequestParam("productName") String productName,HttpServletRequest request,Model model){

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        return getProductByPage(1, user.getUserId(),
                productName,"","","",model,request);
    }



    //取消入驻功能
    @GetMapping("/exitShopping/{userId}")
    public String exitShopping(@PathVariable("userId") Integer userId,Model model,HttpServletRequest request){

        boolean success = indexService.exitShopping(userId);
        return getProductByPage(1, userId,
                "","","","",model,request);
    }

    @GetMapping("/productType/{productTypeId}")
    public String productType(@PathVariable("productTypeId") Integer productTypeId,Model model){

        ProductType productType = productTypeService.selectProductById(productTypeId);
        List<ProductBase> productBaseList = productService.selectProductByTypeId(productType);
        model.addAttribute("productBaseList",productBaseList);
        model.addAttribute("productType",productType);
        return "productType";
    }

    @GetMapping("/purchase")
    public String purchase(){
        System.out.println("quick purchase");
        return "purchase";
    }


    @GetMapping("/accManage")
    public String accManage(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        return "accManage";
    }

    @GetMapping("/switchUser")
    public String switchUser(){
        return "switchUser";
    }
}
